// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

var Take6Model = require("Take6Model");

cc.Class({
    extends: cc.Component,

    properties: {

        unselectedButtonFrame: {
            default: null,
            type: cc.SpriteFrame
        },

        selectedButtonFrame: {
            default: null,
            type: cc.SpriteFrame
        },

        bCreateButtonEnable : {
                default : true,
                visible : false
        },

        nPlayerCount : {
                default : 3,
                visible : false
        },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad : function () 
    {
        this.node.on('event_create_room_success', function (event)
        {
            var nRoomID = event.detail.nRoomID;
            var labelNode = this.node.getChildByName('roomNumLabel');
            labelNode.getComponent(cc.Label).string = nRoomID;

            this.bCreateButtonEnable = false;

        }, this);
    },

    onCreateBtnPressed : function ()
    {
        Take6Model.playerCreateRoom(this.nPlayerCount);
    },

    on3PlayerBtnPressed : function ()
    {
        this.nPlayerCount = 3;
        this.unselectAllButtons();

       var btn3Node = this.node.getChildByName('playerCount3Button');
       var btnSprite = btn3Node.getComponent(cc.Sprite);
       btnSprite.spriteFrame = this.selectedButtonFrame;
    },

    on4PlayerBtnPressed : function ()
    {
        this.nPlayerCount = 4;
        this.unselectAllButtons();

       var btn4Node = this.node.getChildByName('playerCount4Button');
       var btnSprite = btn4Node.getComponent(cc.Sprite);
       btnSprite.spriteFrame = this.selectedButtonFrame;
    },

    on5PlayerBtnPressed : function ()
    {
        this.nPlayerCount = 5;
        this.unselectAllButtons();

       var btn5Node = this.node.getChildByName('playerCount5Button');
       var btnSprite = btn5Node.getComponent(cc.Sprite);
       btnSprite.spriteFrame = this.selectedButtonFrame;
    },

    unselectAllButtons : function ()
    {
        var btn3Node = this.node.getChildByName('playerCount3Button');
        var btn4Node = this.node.getChildByName('playerCount4Button');
        var btn5Node = this.node.getChildByName('playerCount5Button');

        var btns = [btn3Node, btn4Node, btn5Node];
        for (var i = 0; i < 3; i++)
        {
            var btnSprite = btns[i].getComponent(cc.Sprite);
            btnSprite.spriteFrame = this.unselectedButtonFrame;
        }
    },

    setRoomNum : function (nRoomID)
    {
        var labelNode = this.node.getChildByName('roomNumLabel');
        labelNode.getComponent(cc.Label).string = nRoomID;

        this.bCreateButtonEnable = false;

        this.node.getChildByName('createRoomButton').active = false;
        this.node.getChildByName('exitButton').active = false;

        this.node.getChildByName('playerCount3Button').getComponent(cc.Sprite).getComponent(cc.Button).enabled = false;
        this.node.getChildByName('playerCount4Button').getComponent(cc.Sprite).getComponent(cc.Button).enabled = false;
        this.node.getChildByName('playerCount5Button').getComponent(cc.Sprite).getComponent(cc.Button).enabled = false;
    
        var waitingLabelNode = this.node.getChildByName('waitingLabel');
        waitingLabelNode.oppacity = 0;
        waitingLabelNode.active = true;

        var action1 = cc.fadeIn(0.5);
        var action2 = cc.delayTime(1.0);
        var action3 = cc.fadeOut(0.3);
        var action4 = cc.delayTime(1.0);
        var actionUnion = cc.repeatForever(cc.sequence(action1, action2, action3, action4));

        waitingLabelNode.runAction(actionUnion);
    },

    onExitButtonPressed : function ()
    {
        this.node.removeFromParent();
    },

    start () {

    },

    // update (dt) {},
});
